package sdstcom.lbq.one.lbq.Fragment;

import android.content.Intent;
import android.os.Bundle;

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;


import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;


import com.example.antivity.EssayActivity;
import com.example.antivity.WenActivity;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;


import java.util.LinkedList;

import sdstcom.lbq.one.R;
import sdstcom.lbq.one.lbq.Databeans.Essaybean;
import sdstcom.lbq.one.lbq.uilts.Jsontask;
import sdstcom.lbq.one.lbq.adapter.EssayAdapter;


/**
 * Created by Administrator on 2017/1/6 0006.
 */

public class Essayfragment extends Fragment implements View.OnClickListener {
    private View view;
    private PullToRefreshListView plv;
    private String id;
    private EssayAdapter adapter;
    private LinkedList<Essaybean.DataBean> list;
    private Button search;
    public static Essayfragment Initfragment() {
        Essayfragment essayfragment = new Essayfragment();
        return essayfragment;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.essayfragment_item, container, false);
        search= (Button) view.findViewById(R.id.essay_search);
        plv = (PullToRefreshListView) view.findViewById(R.id.essay_listview);
        list = new LinkedList<>();
        adapter = new EssayAdapter(getActivity(), list);
        plv.setAdapter(adapter);
        Jsontask jsontask = (Jsontask) new Jsontask(getActivity(), new Jsontask.CallBack() {
            @Override
            public void sendStr(String var1) {
                Essaybean bean = new Gson().fromJson(var1, Essaybean.class);
                list.addAll(bean.getData());
                id = bean.getData().get(bean.getData().size() - 1).getId();
                adapter.notifyDataSetChanged();
            }
        }).execute("http://v3.wufazhuce.com:8000/api/channel/reading/more/0?platform=android&version=4.0.2&uuid=ffffffff-acdc-5a02-0a04-a6b66eed847f&channel=91");
        plv.setMode(PullToRefreshBase.Mode.BOTH);
        plv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                Jsontask jsontask = (Jsontask) new Jsontask(getActivity(), new Jsontask.CallBack() {
                    @Override
                    public void sendStr(String var1) {
                        Essaybean bean = new Gson().fromJson(var1, Essaybean.class);
                        if (!bean.getData().get(bean.getData().size() - 1).getId().equals(id)) {
                            list.addAll(0, bean.getData());
                            adapter.notifyDataSetChanged();
                        }
                        plv.onRefreshComplete();
                    }
                }).execute("http://v3.wufazhuce.com:8000/api/channel/reading/more/0?platform=android&version=4.0.2&uuid=ffffffff-acdc-5a02-0a04-a6b66eed847f&channel=91");
            }
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                Jsontask jsontask = (Jsontask) new Jsontask(getActivity(), new Jsontask.CallBack() {
                    @Override
                    public void sendStr(String var1) {
                        Essaybean bean = new Gson().fromJson(var1, Essaybean.class);
                        list.addAll(bean.getData());
                        id = bean.getData().get(bean.getData().size() - 1).getId();
                        adapter.notifyDataSetChanged();
                        plv.onRefreshComplete();
                    }
                }).execute("http://v3.wufazhuce.com:8000/api/channel/reading/more/" + id + "?platform=android&version=4.0.2&uuid=ffffffff-acdc-5a02-0a04-a6b66eed847f&channel=91");
            }
        });
        plv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (list.get(position-1).getCategory().equals("1"))
                {

                    startActivity(new Intent(getActivity(), EssayActivity.class).putExtra("itemid",list.get(position-1).getItem_id()));
                }
                else
                {
                    startActivity(new Intent(getActivity(), WenActivity.class).putExtra("itemid",list.get(position-1).getItem_id()));
                }
            }
        });
      search.setOnClickListener(this);
        return view;
    }
    @Override
    public void onClick(View v) {
      Toast.makeText(getActivity(),"搜索模块优化中。。。敬请期待",Toast.LENGTH_SHORT).show();
    }

}
